package com.atguigu.spzx.manage.service.Impl;


import com.atguigu.spzx.common.exception.GuiguException;
import com.atguigu.spzx.manage.mapper.EventAimMapper;
import com.atguigu.spzx.manage.mapper.EventContentMapper;
import com.atguigu.spzx.manage.mapper.EventMapper;
import com.atguigu.spzx.manage.service.EventService;
import com.atguigu.spzx.model.dto.system.EventDto;
import com.atguigu.spzx.model.entity.system.Event;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class EventServiceImpl implements EventService {
    @Autowired
    private EventMapper eventMapper;
    @Autowired
    private EventAimMapper eventAimMapper;
    @Autowired
    private EventContentMapper eventContentMapper;
    @Override
    public PageInfo<Event> findByPage(EventDto eventDto, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum , pageSize);
        List<Event> eventList = eventMapper.findByPage(eventDto) ;
        PageInfo pageInfo = new PageInfo(eventList) ;
        return pageInfo;
    }

    @Override
    public void saveEvent(Event event) {
        //查询活动名,判断活动名存在,则用户名重复抛出异常
        Event dbEvent = eventMapper.findByEventName(event.getEventName()) ;
        if(dbEvent !=null) {
            throw new GuiguException(ResultCodeEnum.Event_NAME_IS_EXISTS) ;
        }
        //设置活动未开启
        event.setStatus(2);
        //添加报名表
        eventMapper.saveEvent(event);
        //查询报名表的aim_id和content_id
        Event eventNew=eventMapper.findByEventName(event.getEventName());
        //添加目的表
        eventAimMapper.saveEventAim(eventNew.getId(),event.getAim());

        //添加内容表
        eventContentMapper.saveEventContent(eventNew.getId(),event.getContent());

    }

    @Override
    public void updateEvent(Event event) {
        //更新没有回显
        // 根据输入的用户名查询用户


        //修改目的表
        eventAimMapper.updateEventAim(event.getId(),event.getAim());

        //修改内容表
        eventContentMapper.updateEventContent(event.getId(),event.getContent());
        //修改活动表
        eventMapper.updateById(event);
    }
    //发布活动
    @Override
    public void startById(Long id) {
        Event event=eventMapper.selectById(id);
        event.setStatus(1);
        eventMapper.updateById(event);

        //改报名表为1
        //OOTD

    }
    //关闭活动
    @Override
    public void endById(Long id) {
        Event event=eventMapper.selectById(id);
        log.info("event包括{}",event);
        event.setStatus(2);
        log.info("现在的event包括{}",event);
        eventMapper.updateById(event);
        //改报名表为0
        //OOTD
    }

    @Override
    public void deleteById(Long id) {

        //删除目的表
        eventAimMapper.deleteById(id);

        //删除内容表
        eventContentMapper.deleteById(id);
        //删除活动表
        eventMapper.deleteById(id);
    }


}
